package com.ynet.middleground.riskmanage.model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ifp.util.core.DateUtil;
import com.ynet.core.common.BaseReqObjPager;
import com.ynet.core.common.PagerInfo;
import com.ynet.core.exception.BusinessException;
import com.ynet.middleground.riskmanage.bean.*;
import com.ynet.middleground.riskmanage.dao.*;
import com.ynet.middleground.riskmanage.dto.*;
import com.ynet.middleground.riskmanage.entity.*;
import com.ynet.middleground.riskmanage.external.qcc.ApiHelper;
import com.ynet.middleground.riskmanage.external.qcc.QccUriEnum;
import com.ynet.middleground.riskmanage.utils.BeanUtil;

/**
 * @author liulx
 * @description 企查查业务处理类
 * @date 2020-08-04 21:07
 */
@Component
public class QccModel {

    @Autowired
    private ApiHelper apiHelper;

    @Autowired
    private Mapper mapper;

    @Autowired
    private QichachaLogMapper logMapper;

    @Autowired
    private QichachaAicDetailMapper aicDetailMapper;

    @Autowired
    private QichachaAicDetailOriginalNameMapper aicDetailOriginalNameMapper;

    @Autowired
    private QichachaInvestmentMapper investmentMapper;

    @Autowired
    private QichachaPartnerMapper partnerMapper;

    @Autowired
    private QichachaEmployeeMapper employeeMapper;

    @Autowired
    private QichachaBranchMapper branchMapper;

    /**
     * 数据是否存在(1-存在，0-不存在)
     **/
    private static final Integer VERIFY_RESULT = 1;

    /**
     * 企查查统计查询，渠道专用
     *
     * @param reqPager 查询请求信息
     * @return 统计查询结果集合
     */
    public List<QccStatisticDTO> tatisticQueryForChannel(QccStatisticQueryReqPager reqPager) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateUtil.FORTER_DATE);
        if (StringUtils.isBlank(reqPager.getQueryChannel())) {
            throw new BusinessException("查询渠道不能为空", "ECRM0001");
        }
        if (reqPager.getPagerInfo().getPageNum() <= 0) {
            throw new BusinessException("pageNum 必须从1开始递增", "ECRM0001");
        }

        if (StringUtils.isNotBlank(reqPager.getRequestTimeStart())) {
            try {
                simpleDateFormat.parse(reqPager.getRequestTimeStart());
                reqPager.setRequestTimeStart(reqPager.getRequestTimeStart() + " 00:00:00");
            } catch (ParseException e) {
                throw new BusinessException("调用时间起格式不正确", "ECRM0003");
            }
        }
        if (StringUtils.isNotBlank(reqPager.getRequestTimeEnd())) {
            try {
                simpleDateFormat.parse(reqPager.getRequestTimeEnd());
                reqPager.setRequestTimeEnd(reqPager.getRequestTimeEnd() + " 23:59:59");
            } catch (ParseException e) {
                throw new BusinessException("调用时间止格式不正确", "ECRM0003");
            }
        }

        Long queryCount = logMapper.tatisticQueryCount(reqPager);
        reqPager.getPagerInfo().setTotal(queryCount);
        return logMapper.tatisticQuery(reqPager, reqPager.getPagerInfo());
    }

    /**
     * 企查查统计查询，内管专用
     *
     * @param reqPager 查询请求信息
     * @return 统计查询结果集合
     */
    public List<QccStatisticDTO> tatisticQueryForPortAdmin(QccStatisticQueryReqPager reqPager) {
        if (reqPager.getPagerInfo().getPageNum() <= 0) {
            throw new BusinessException("pageNum 必须从1开始递增", "ECRM0001");
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateUtil.FORTER_DATE);

        if (StringUtils.isNotBlank(reqPager.getRequestTimeStart())) {
            try {
                simpleDateFormat.parse(reqPager.getRequestTimeStart());
                reqPager.setRequestTimeStart(reqPager.getRequestTimeStart() + " 00:00:00");
            } catch (ParseException e) {
                throw new BusinessException("调用时间起格式不正确", "ECRM0003");
            }
        }
        if (StringUtils.isNotBlank(reqPager.getRequestTimeEnd())) {
            try {
                simpleDateFormat.parse(reqPager.getRequestTimeEnd());
                reqPager.setRequestTimeEnd(reqPager.getRequestTimeEnd() + " 23:59:59");
            } catch (ParseException e) {
                throw new BusinessException("调用时间止格式不正确", "ECRM0003");
            }
        }

        Long queryCount = logMapper.tatisticQueryCount(reqPager);
        reqPager.getPagerInfo().setTotal(queryCount);
        return logMapper.tatisticQuery(reqPager, reqPager.getPagerInfo());
    }

    /************* 工商信息类 *************/
    /**
     * 企业关键字精确获取详细信息 (Basic)
     * 
     * @param req 查询企业基础数据请求信息
     * @return 企业工商详情信息
     */
    @Transactional(rollbackFor = Exception.class)
    public QichachaAicDetailDTO getBasicDetailsByName(CompanyBasicQueryReq req) {
        Map<String, Object> params = new HashMap<>(2);
        params.put("keyWord", req.getKeyword());
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_BASIC_DETAILS_BY_NAME, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (!code.equals(ApiHelper.STATUS_SUCCESS)) {
            throw new BusinessException(message, String.valueOf(code));
        }
        JSONObject result = apiResult.getJSONObject("Result");
        QichachaAicDetail aicDetail = result.toJavaObject(QichachaAicDetail.class);
        BeanUtil.setGenericInfo(aicDetail, req);
        aicDetail.setKeyword(req.getKeyword());
        aicDetailMapper.insert(aicDetail);
        JSONArray originalNameArray = result.getJSONArray("OriginalName");
        List<QichachaAicDetailOriginalName> originalNameList = null;
        if (originalNameArray != null) {
            originalNameList = originalNameArray.toJavaList(QichachaAicDetailOriginalName.class);
            for (QichachaAicDetailOriginalName name : originalNameList) {
                name.setDetailId(aicDetail.getId());
                aicDetailOriginalNameMapper.insert(name);
            }
        }
        QichachaAicDetailDTO aicDetailDTO = mapper.map(aicDetail, QichachaAicDetailDTO.class);
        List<QichachaAicOriginalNameDTO> originalNameDTOList =
            BeanUtil.mapList(mapper, originalNameList, QichachaAicOriginalNameDTO.class);
        aicDetailDTO.setOriginalNameList(originalNameDTOList);
        return aicDetailDTO;
    }

    /**
     * 查询企业对外投资列表
     * 
     * @param req 企查查分页查询通用基础请求信息
     * @return 对外投资列表
     */
    public List<InvestmentDTO> listInvestment(CompanyBaseQueryReqPager req) {
        Map<String, Object> params = new HashMap<>(4);
        params.put("searchKey", req.getSearchKey());
        setReqPagingInfo(params, req);
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_INVESTMENT_LIST, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (!code.equals(ApiHelper.STATUS_SUCCESS)) {
            throw new BusinessException(message, String.valueOf(code));
        }
        setPageInfo(apiResult, req);
        JSONArray jsonArray = apiResult.getJSONObject("Result").getJSONArray("Data");
        List<QichachaInvestment> investmentList = null;
        if (jsonArray != null) {
            investmentList = jsonArray.toJavaList(QichachaInvestment.class);
            for (QichachaInvestment investment : investmentList) {
                BeanUtil.setGenericInfo(investment, req);
                investmentMapper.insert(investment);
            }
        }
        Integer verifyResult = apiResult.getJSONObject("Result").getInteger("VerifyResult");
        List<InvestmentDTO> investmentDTOList = new ArrayList<>();
        if (verifyResult == VERIFY_RESULT) {
            investmentDTOList = jsonArray.toJavaList(InvestmentDTO.class);;
        }
        return investmentDTOList;
    }

    /**
     * 查询股东列表信息
     *
     * @param req 企查查分页查询通用基础请求信息
     * @return 股东列表
     */
    public List<PartnerDTO> listPartner(CompanyBaseQueryReqPager req) {
        Map<String, Object> params = new HashMap<>(4);
        params.put("searchKey", req.getSearchKey());
        setReqPagingInfo(params, req);
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_PARTNER_LIST, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (!code.equals(ApiHelper.STATUS_SUCCESS)) {
            throw new BusinessException(message, String.valueOf(code));
        }
        setPageInfo(apiResult, req);
        JSONArray jsonArray = apiResult.getJSONArray("Result");
        List<QichachaPartner> partnerList = null;
        if (jsonArray != null) {
            partnerList = jsonArray.toJavaList(QichachaPartner.class);
            for (QichachaPartner partner : partnerList) {
                BeanUtil.setGenericInfo(partner, req);
                partnerMapper.insert(partner);
            }
        }
        return BeanUtil.mapList(mapper, partnerList, PartnerDTO.class);
    }

    /**
     * 查询主要人员列表信息
     *
     * @param req 企查查分页查询通用基础请求信息
     * @return 主要人员列表
     */
    public List<EmployeeDTO> listEmployee(CompanyBaseQueryReqPager req) {
        Map<String, Object> params = new HashMap<>(4);
        params.put("searchKey", req.getSearchKey());
        setReqPagingInfo(params, req);
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_EMPLOYEE_LIST, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (!code.equals(ApiHelper.STATUS_SUCCESS)) {
            throw new BusinessException(message, String.valueOf(code));
        }
        setPageInfo(apiResult, req);
        JSONArray jsonArray = apiResult.getJSONArray("Result");
        List<QichachaEmployee> employeeList = null;
        if (jsonArray != null) {
            employeeList = jsonArray.toJavaList(QichachaEmployee.class);
            for (QichachaEmployee employee : employeeList) {
                BeanUtil.setGenericInfo(employee, req);
                employeeMapper.insert(employee);
            }
        }
        return BeanUtil.mapList(mapper, employeeList, EmployeeDTO.class);
    }

    /**
     * 查询分支结构
     *
     * @param req 企查查分页查询通用基础请求信息
     * @return 分支结构列表
     */
    public List<BranchDTO> listBranch(CompanyBaseQueryReqPager req) {
        Map<String, Object> params = new HashMap<>(4);
        params.put("searchKey", req.getSearchKey());
        setReqPagingInfo(params, req);
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_BRANCH_LIST, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (!code.equals(ApiHelper.STATUS_SUCCESS)) {
            throw new BusinessException(message, String.valueOf(code));
        }
        setPageInfo(apiResult, req);
        JSONArray jsonArray = apiResult.getJSONArray("Result");
        List<QichachaBranch> list = new ArrayList<>();
        if (jsonArray != null) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                JSONObject operInfo = jsonObject.getJSONObject("OperInfo");
                QichachaBranch branch = jsonArray.getObject(i, QichachaBranch.class);
                branch.setOperinfoKeyNo(operInfo.getString("KeyNo"));
                branch.setOperinfoName(operInfo.getString("Name"));
                BeanUtil.setGenericInfo(branch, req);
                branchMapper.insert(branch);
                list.add(branch);
            }
        }
        return BeanUtil.mapList(mapper, list, BranchDTO.class);
    }

    /**
     * 变更记录 <br>
     *
     * @param req 查询企业基础数据请求信息
     * @return 变更记录列表
     */
    public List<ChangeDTO> listChange(CompanyBasicQueryReq req) {
        Map<String, Object> params = new HashMap<>(4);
        params.put("searchKey", req.getKeyword());
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_CHANGE_LIST, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (!code.equals(ApiHelper.STATUS_SUCCESS)) {
            throw new BusinessException(message, String.valueOf(code));
        }
        JSONArray jsonArray = apiResult.getJSONArray("Result");
        List<ChangeDTO> list = new ArrayList<>();
        if (jsonArray != null) {
            for (int i = 0; i < jsonArray.size(); i++) {
                ChangeDTO changeDTO = new ChangeDTO();
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String projectName = jsonObject.getString("ProjectName");
                String changeDate = jsonObject.getString("ChangeDate");
                changeDTO.setProjectName(projectName);
                changeDTO.setChangeDate(changeDate);
                JSONArray beforeList = jsonObject.getJSONArray("BeforeList");
                JSONArray afterList = jsonObject.getJSONArray("AfterList");
                changeDTO.setBeforeList(beforeList.toJavaList(String.class));
                changeDTO.setAfterList(afterList.toJavaList(String.class));
                JSONArray beforeInfoList = jsonObject.getJSONArray("BeforeInfoList");
                JSONArray afterInfoList = jsonObject.getJSONArray("AfterInfoList");
                changeDTO.setBeforeInfoList(beforeInfoList.toJavaList(ChangeContentDTO.class));
                changeDTO.setAfterInfoList(afterInfoList.toJavaList(ChangeContentDTO.class));
                list.add(changeDTO);
            }
        }
        return list;
    }

    /**
     * 设置企查查分页请求信息参数
     * 
     * @param params 请求参数集合
     * @param reqPager 当前分页请求类
     */
    protected void setReqPagingInfo(Map<String, Object> params, BaseReqObjPager reqPager) {
        params.put("pageIndex", reqPager.getPagerInfo().getPageNum());
        params.put("pageSize", reqPager.getPagerInfo().getPageSize());
    }

    /**
     * 如果 api 结果返回分页信息，则将分页结果重新添加到请求分页对象中
     * 
     * @param apiResult 企查查 api 结果
     * @param reqPager 请求分页信息
     */
    protected void setPageInfo(JSONObject apiResult, BaseReqObjPager reqPager) {
        JSONObject paging = apiResult.getJSONObject("Paging");
        if (paging == null) {
            return;
        }
        PagerInfo pagerInfo = reqPager.getPagerInfo();
        pagerInfo.setTotal(paging.getInteger("TotalRecords"));
    }

    /************* 法律诉讼类 *************/
    /**
     * 开庭公告查询
     * 
     * @param req 查询开庭公告请求信息
     * @return 开庭公告信息
     * @author songyf 2020-08-07
     */
    @Transactional(rollbackFor = Exception.class)
    public List<QccCourtNoticeDTO> searchCourtNotice(CourtNoticeQueryReqPager req) {
        Map<String, Object> params = new HashMap<>(2);
        params.put("searchKey", req.getSearchKey());
        if(StringUtils.isNotBlank(req.getCaseIdentity())){
            params.put("caseIdentity", req.getCaseIdentity());
        }
        setReqPagingInfo(params, req);
        params = apiHelper.reqParamsProc(params);
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.SEARCH_COURT_NOTICE, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (code != ApiHelper.STATUS_SUCCESS) {
            throw new BusinessException(message, String.valueOf(code));
        }
        setPageInfo(apiResult, req);
        JSONArray result = apiResult.getJSONObject("Result").getJSONArray("Data");
        HashMap<String, String> keyMap = new HashMap<>();
        keyMap.put("Id", "innerId");
        //JSONArray resJson = BeanUtil.changeJsonArr(result, keyMap);
        //List<QichachaCourtAnnoResp> courtAnnoRespList = resJson.toJavaList(QichachaCourtAnnoResp.class);
        // if (courtAnnoRespList.size() > 0) {
        // for (int i = 0; i < courtAnnoRespList.size(); i++) {
        // BeanUtil.setGenericInfo(courtAnnoRespList.get(i), req);
        // courtAnnoRespMapper.insert(courtAnnoRespList.get(i));
        // }
        // }
        //List<QccCourtNoticeDTO> courtNoticeDTOList = result.toJavaList(QccCourtNoticeDTO.class);
        Integer verifyResult = apiResult.getJSONObject("Result").getInteger("VerifyResult");
        List<QccCourtNoticeDTO> courtNoticeDTOList = new ArrayList<>();
        if (verifyResult == VERIFY_RESULT) {
            courtNoticeDTOList = result.toJavaList(QccCourtNoticeDTO.class);;
        }
        return courtNoticeDTOList;
    }

    /**
     * 开庭公告详情查询
     * 
     * @param req 查询开庭公告详情请求信息
     * @return 开庭公告详情信息
     * @author songyf 2020-08-10
     */
    @Transactional(rollbackFor = Exception.class)
    public QccCourtNoticeDetailDTO searchCourtNoticeDetail(CourtNoticeDetailQueryReq req) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", req.getId());
        params = apiHelper.reqParamsProc(params);
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_COURT_NOTICE_INFO, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (code != ApiHelper.STATUS_SUCCESS) {
            throw new BusinessException(message, String.valueOf(code));
        }
        JSONObject result = apiResult.getJSONObject("Result");
        QccCourtNoticeDetailDTO courtNoticeDetailDTO = result.toJavaObject(QccCourtNoticeDetailDTO.class);;
        // QichachaCourtAnnoDetailResp courtAnnoDetailResp =
        // mapper.map(courtNoticeDetailDTO, QichachaCourtAnnoDetailResp.class);
        // BeanUtil.setGenericInfo(courtAnnoDetailResp, req);
        // courtAnnoDetailRespMapper.insert(courtAnnoDetailResp);
        // Integer detailId = courtAnnoDetailRespMapper.insert(courtAnnoDetailResp);
        // List<QichachaCourtAnnoDetailProsecutor> prosecutorList = BeanUtil.mapList(mapper,
        // courtNoticeDetailDTO.getProsecutor(), QichachaCourtAnnoDetailProsecutor.class);
        // for(int i=0;i<prosecutorList.size();i++) {
        // QichachaCourtAnnoDetailProsecutor prosecutor = prosecutorList.get(i);
        // BeanUtil.setGenericInfo(prosecutor, req);
        // prosecutor.setDetailId(detailId);
        // prosecutorMapper.insert(prosecutor);
        // }
        // List<QichachaCourtAnnoDetailDefendant> defendantList = BeanUtil.mapList(mapper,
        // courtNoticeDetailDTO.getDefendant(), QichachaCourtAnnoDetailDefendant.class);
        // for(int i=0;i<defendantList.size();i++) {
        // QichachaCourtAnnoDetailDefendant defendant = defendantList.get(i);
        // BeanUtil.setGenericInfo(defendant, req);
        // defendant.setDetailId(detailId);
        // defendantMapper.insert(defendant);
        // }
        return courtNoticeDetailDTO;
    }

    /**
     * 裁判文书查询
     * 
     * @param req 查询裁判文书
     * @return 裁判文书信息
     * @author songyf 2020-08-11
     */
    public List<JudgmentDocDTO> searchJudgmentDoc(JudgmentDocQueryReqPager req) {
        Map<String, Object> params = new HashMap<>();
        params.put("searchKey", req.getSearchKey());
        if (StringUtils.isNotEmpty(req.getCaseIdentity())) {
            params.put("caseIdentity", req.getCaseIdentity());
        }
        if (StringUtils.isNotEmpty(req.getPubYear())) {
            params.put("pubYear", req.getPubYear());
        }
        setReqPagingInfo(params, req);
        params = apiHelper.reqParamsProc(params);
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.SEARCH_JUDGMENT_DOC, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (code != ApiHelper.STATUS_SUCCESS) {
            throw new BusinessException(message, String.valueOf(code));
        }
        setPageInfo(apiResult, req);
        JSONArray result = apiResult.getJSONObject("Result").getJSONArray("Data");
        List<JudgmentDocDTO> judgmentDocDTO = result.toJavaList(JudgmentDocDTO.class);;
        return judgmentDocDTO;
    }

    /**
     * 立案信息列表查询
     * 
     * @param req 企查查分页查询基础请求信息
     * @return 立案信息列表
     * @author liulx
     */
    public List<CaseFilingDTO> listCaseFiling(CourtNoticeQueryReqPager req) {
        Map<String, Object> params = new HashMap<>(4);
        params.put("searchKey", req.getSearchKey());
        if(StringUtils.isNotBlank(req.getCaseIdentity())){
            params.put("caseIdentity", req.getCaseIdentity());
        }
        setReqPagingInfo(params, req);
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.CASE_FILING_LIST, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (!code.equals(ApiHelper.STATUS_SUCCESS)) {
            throw new BusinessException(message, String.valueOf(code));
        }
        setPageInfo(apiResult, req);
        JSONArray jsonArray = apiResult.getJSONObject("Result").getJSONArray("Data");
        List<CaseFilingDTO> list = new ArrayList<>();
        if (jsonArray != null) {
            list = jsonArray.toJavaList(CaseFilingDTO.class);
        }
        return list;
    }

    /**
     * 查询立案详情信息
     *
     * @param req 企查查基本查询请求信息
     * @return 立案详情信息
     * @author liulx
     */
    public CaseFilingDetailDTO getCaseFiling(QccBaseQueryReq req) {
        Map<String, Object> params = new HashMap<>(4);
        params.put("id", req.getId());
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.CASE_FILING_DETAIL, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (!code.equals(ApiHelper.STATUS_SUCCESS)) {
            throw new BusinessException(message, String.valueOf(code));
        }
        JSONObject result = apiResult.getJSONObject("Result");
        return result.toJavaObject(CaseFilingDetailDTO.class);
    }

    /**
     * 查询股权出质列表
     * 
     * @param req 企查查分页查询基础请求信息
     * @return 股权出质结果信息
     * @author liulx
     */
    public EquityPledgedResultDTO listEquityPledged(CompanyBaseQueryReqPager req) {
        Map<String, Object> params = new HashMap<>(4);
        params.put("searchKey", req.getSearchKey());
        setReqPagingInfo(params, req);
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_EQUITY_PLEDGED_CHECK_LIST, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (!code.equals(ApiHelper.STATUS_SUCCESS)) {
            throw new BusinessException(message, String.valueOf(code));
        }
        setPageInfo(apiResult, req);
        JSONObject result = apiResult.getJSONObject("Result");
        Integer verifyResult = result.getInteger("VerifyResult");
        JSONArray jsonArray = result.getJSONArray("Data");
        List<EquityPledgedDTO> list = new ArrayList<>();
        if (jsonArray != null) {
            list = jsonArray.toJavaList(EquityPledgedDTO.class);
        }
        EquityPledgedResultDTO dto = new EquityPledgedResultDTO();
        dto.setVerifyResult(verifyResult);
        dto.setData(list);
        return dto;
    }

    /**
     * 行政处罚核查列表查询
     * 
     * @param req 企查查分页查询基础请求信息
     * @return 行政处罚核查列表
     * @author songyf
     */
    public List<AdminPenaltyCheckDTO> getAdministrativePenaltiesList(CompanyBaseQueryReqPager req) {
        Map<String, Object> params = new HashMap<>(4);
        params.put("searchKey", req.getSearchKey());
        setReqPagingInfo(params, req);
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_ADMINISTRATIVE_PENALT, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        JSONObject result = apiResult.getJSONObject("Result");
        if (!code.equals(ApiHelper.STATUS_SUCCESS)) {
            throw new BusinessException(message, String.valueOf(code));
        }
        setPageInfo(apiResult, req);
        JSONArray jsonArray = result.getJSONArray("Data");
        List<AdminPenaltyCheckDTO> list = new ArrayList<>();
        if (jsonArray != null) {
            list = jsonArray.toJavaList(AdminPenaltyCheckDTO.class);
        }
        return list;
    }
    
    /**
     * 查询行政处罚详情信息
     * 
     * @param req 企查查基本查询请求信息
     * @return 行政处罚详情信息
     * @author liulx
     */
    public AdministrativePenaltiesDetailDTO getAdministrativePenaltiesDetail(QccBaseQueryReq req) {
        Map<String, Object> params = new HashMap<>(4);
        params.put("id", req.getId());
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_ADMINISTRATIVE_PENALT_DETAIL, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (!code.equals(ApiHelper.STATUS_SUCCESS)) {
            throw new BusinessException(message, String.valueOf(code));
        }
        JSONObject result = apiResult.getJSONObject("Result");
        return result.toJavaObject(AdministrativePenaltiesDetailDTO.class);
    }

    /**
     * 裁判文书详情查询
     * 
     * @param req 查询裁判文书详情
     * @return 裁判文书详情信息
     * @author songyf 2020-08-11
     */
    public JudgmentDocDetailDTO searchJudgmentDocDetail(JudgmentDocDetailQueryReq req) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", req.getId());
        params = apiHelper.reqParamsProc(params);
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_JUDGMENT_DETAIL, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (code != ApiHelper.STATUS_SUCCESS) {
            throw new BusinessException(message, String.valueOf(code));
        }
        JSONObject result = apiResult.getJSONObject("Result");
        JudgmentDocDetailDTO judgmentDocDTO = result.toJavaObject(JudgmentDocDetailDTO.class);;
        return judgmentDocDTO;
    }

    /**
     * 法院公告列表信息查询
     * 
     * @param req 查询法院公告列表信息
     * @return 法院公告列表信息
     * @author songyf 2020-08-11
     */
    public List<CourtAnnouncementDTO> searchCourtAnnouncement(CompanyBaseQueryReqPager req) {
        Map<String, Object> params = new HashMap<>();
        params.put("searchKey", req.getSearchKey());
        setReqPagingInfo(params, req);
        params = apiHelper.reqParamsProc(params);
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.SEARCH_COURT_ANNOUNCEMENT, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (code != ApiHelper.STATUS_SUCCESS) {
            throw new BusinessException(message, String.valueOf(code));
        }
        setPageInfo(apiResult, req);
        JSONArray result = apiResult.getJSONObject("Result").getJSONArray("Data");
        Integer verifyResult = apiResult.getJSONObject("Result").getInteger("VerifyResult");
        List<CourtAnnouncementDTO> courtAnnouncementDTOList = new ArrayList<>();
        if (verifyResult == VERIFY_RESULT) {
            courtAnnouncementDTOList = result.toJavaList(CourtAnnouncementDTO.class);;
        }
        return courtAnnouncementDTOList;
    }

    /**
     * 法院公告详情查询
     * 
     * @param req 法院公告详情请求信息
     * @return 法院公告详情信息
     * @author songyf 2020-08-12
     */
    public QccCourtAnnouncementDetailDTO searchCourtAnnouncementDetail(CourtAnnouncementDetailQueryReq req) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", req.getId());
        params = apiHelper.reqParamsProc(params);
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.SEARCH_COURT_ANNOUNCEMENT_DETAIL, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (code != ApiHelper.STATUS_SUCCESS) {
            throw new BusinessException(message, String.valueOf(code));
        }
        JSONObject result = apiResult.getJSONObject("Result");
        QccCourtAnnouncementDetailDTO courtAnnouncementDetailDTO =
            result.toJavaObject(QccCourtAnnouncementDetailDTO.class);;
        return courtAnnouncementDetailDTO;
    }

    /**
     * 被执行人详情信息查询
     * 
     * @param req 企查查基本查询请求信息
     * @return 被执行人详情信息
     * @author liulx
     * @date 2020-08-18 17:58:20
     */
    public ZhiXingDTO getZhiXingDetail(QccBaseQueryReq req) {
        Map<String, Object> params = new HashMap<>(2);
        params.put("id", req.getId());
        params = apiHelper.reqParamsProc(params);
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_ZHI_XING_CHECK_DETAIL, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (code != ApiHelper.STATUS_SUCCESS) {
            throw new BusinessException(message, String.valueOf(code));
        }
        JSONObject result = apiResult.getJSONObject("Result");
        return result.toJavaObject(ZhiXingDTO.class);
    }

    /**
     * 被执行人核查信息
     * 
     * @param req 企查查分页查询基础请求信息
     * @return 被执行人核查信息列表
     */
    public ZhiXingCheckResultDTO listZhixing(CompanyBaseQueryReqPager req) {
        Map<String, Object> params = new HashMap<>();
        params.put("searchKey", req.getSearchKey());
        setReqPagingInfo(params, req);
        params = apiHelper.reqParamsProc(params);
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_ZHI_XING_CHECK_LIST, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (code != ApiHelper.STATUS_SUCCESS) {
            throw new BusinessException(message, String.valueOf(code));
        }
        setPageInfo(apiResult, req);
        JSONObject result = apiResult.getJSONObject("Result");
        Integer verifyResult = result.getInteger("VerifyResult");
        JSONArray data = result.getJSONArray("Data");
        List<ZhiXingCheckDTO> zhiXingDTOList = data == null ? null : data.toJavaList(ZhiXingCheckDTO.class);
        ZhiXingCheckResultDTO resultDTO = new ZhiXingCheckResultDTO();
        resultDTO.setVerifyResult(verifyResult);
        resultDTO.setData(zhiXingDTOList);
        return resultDTO;
    }

    /**
     * 失信核查查询
     * 
     * @param req 查询失信核查
     * @return 失信核查
     * @author songyf 2020-08-12
     */
    public ShiXinListDTO searchShiXinList(ShiXinListQueryReqPager req) {
        Map<String, Object> params = new HashMap<>();
        params.put("searchKey", req.getSearchKey());

        setReqPagingInfo(params, req);
        params = apiHelper.reqParamsProc(params);
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.SEARCH_SHI_XIN, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (code != ApiHelper.STATUS_SUCCESS) {
            throw new BusinessException(message, String.valueOf(code));
        }
        setPageInfo(apiResult, req);
        JSONObject result = apiResult.getJSONObject("Result");
        ShiXinListDTO shiXinDTOList = result.toJavaObject(ShiXinListDTO.class);;
        return shiXinDTOList;
    }

    /**
     * 失信详情查询
     * 
     * @param req 查询失信详情
     * @return 失信详情
     * @author songyf 2020-08-19
     */
    public ShiXinDTO searchShiXinDetail(ShiXinDetailQueryReq req) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", req.getId());
        params = apiHelper.reqParamsProc(params);
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.SEARCH_SHI_XIN_DETAIL, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (code != ApiHelper.STATUS_SUCCESS) {
            throw new BusinessException(message, String.valueOf(code));
        }
        JSONObject result = apiResult.getJSONObject("Result");
        ShiXinDTO shiXinDTO = result.toJavaObject(ShiXinDTO.class);;
        return shiXinDTO;
    }

    /**
     * 获取股权冻结信息
     * 
     * @param req 获取股权冻结信息
     * @return 股权冻结信息
     * @author songyf 2020-08-12
     */
    public List<JudicialAssistanceDTO> searchJudicialAssistance(CompanyBaseQueryReqPager req) {
        Map<String, Object> params = new HashMap<>();
        params.put("searchKey", req.getSearchKey());
        setReqPagingInfo(params, req);
        params = apiHelper.reqParamsProc(params);
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_JUDICIAL_ASSISTANCE, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (code != ApiHelper.STATUS_SUCCESS) {
            throw new BusinessException(message, String.valueOf(code));
        }
        setPageInfo(apiResult, req);
        JSONObject result = apiResult.getJSONObject("Result");
        Integer verifyResult = result.getInteger("VerifyResult");
        List<JudicialAssistanceDTO> judicialAssistanceDTOList = new ArrayList<>();
        if (verifyResult == VERIFY_RESULT) {
            judicialAssistanceDTOList = result.getJSONArray("Data").toJavaList(JudicialAssistanceDTO.class);
        }
        return judicialAssistanceDTOList;
    }

    /**
     * 获取股权冻结信息详情
     *
     * @param req 获取股权冻结信息详情
     * @return 股权冻结信息
     * @author songyf 2021-11-29
     */
    public JudicialAssistanceDetailDTO searchJudicialAssistanceDetail(JudicialAssistanceQueryReq req) {
        Map<String, Object> params = new HashMap<>();
        params.put("keyNo", req.getKeyNo());
        params.put("id", req.getId());
        params = apiHelper.reqParamsProc(params);
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_JUDICIAL_ASSISTANCE_DETAIL, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (code != ApiHelper.STATUS_SUCCESS) {
            throw new BusinessException(message, String.valueOf(code));
        }
        JSONObject result = apiResult.getJSONObject("Result");
        JudicialAssistanceDetailDTO judicialAssistanceDetailDTO = result.toJavaObject(JudicialAssistanceDetailDTO.class);
        return judicialAssistanceDetailDTO;
    }
}
